﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using StudyCompanion.Commons.Result;
using StudyCompanion.Core.API;
using StudyCompanion.Core.Enum;
using StudyCompanion.Core.Models;
using StudyCompanion.Core.Share;
using StudyCompanion.Dto.User;
using StudyCompanion.Entity.User;
using StudyCompanion.IServices;
using StudyCompanion.Model.User;

namespace StudyCompanion.WebApi.Controllers;

/// <summary>
///     登录
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class UserController : ApiController
{
    private readonly IUserContext _userContext;
    private readonly IUserService _userService;

    /// <summary>
    /// </summary>
    /// <param name="userService"></param>
    /// <param name="userContext"></param>
    public UserController(IUserService userService
        , IUserContext userContext)
    {
        _userService = userService;
        _userContext = userContext;
    }

    /// <summary>
    ///     学生年级
    /// </summary>
    /// <returns></returns>
    [HttpGet("StudyClass")]
    public CommonResult<List<StudyClassVo>> StudyClass()
        => _userService.StudyClass();

    /// <summary>
    ///     用户信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("UserInfo")]
    public async Task<CommonResult<UserInforVo>> UserInfo()
    {
        var isUser = await _userService.GetUserbyId(_userContext.Id);
        return isUser.Item1 ? isUser.Item2.Adapt<UserInforVo>() : Problem("用户不存在");
    }

    /// <summary>
    ///     更新用户信息
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("UpdateInfor")]
    public async Task<CommonResult<UserInforVo>> UpdateUserInfor(UserInforDto dto)
    {
        if (dto.Identity == IdentidyEnum.Children && !dto.Class.HasValue)
            return Problem("学生年级不能为空");
        var user = await _userService.GetUserbyId(_userContext.Id);
        if (!user.Item1)
            return Problem("用户不存在");
        user.Item2.Identity = (int)dto.Identity;
        user.Item2.Class = dto.Class ?? 0;
        if (!_userService.UpdateUsers(user.Item2))
            return Problem("用户信息更新失败");
        return user.Adapt<UserInforVo>();
    }

    /// <summary>
    ///     设置/修改登录密码
    ///     密码格式：大小写字母和数字组成，6到16位
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("UpdatePassword")]
    public async Task<CommonResult> ChangeUserPassword(ChangeUserPasswordDto dto)
    {
        if (string.IsNullOrEmpty(dto.Password)) return Problem("密码不可为空");
        return await _userService.ChangeUserPassword(_userContext.Id, dto);
    }

    /// <summary>
    ///     更新用户昵称
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("UpdateNickName")]
    public async Task<CommonResult> UpdateUserName(SimpleDto<string> input)
    {
        if (string.IsNullOrEmpty(input.Value))
            return Problem("用户昵称不能为空");
        if (input.Value.Length > 20)
            return Problem("用户昵称长度太长");
        var user = await _userService.GetUserbyId(_userContext.Id);
        if (!user.Item1)
            return Problem("用户不存在");
        user.Item2.NickName = input.Value;
        return _userService.UpdateUsers(user.Item2) ? Success() : Problem("更新昵称失败");
    }

    /// <summary>
    ///     更新头像
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("UpdateAvatar")]
    public async Task<CommonResult> UpdateUserAvatar(SimpleDto<string> input)
    {
        if (string.IsNullOrEmpty(input.Value))
            return Problem("参数错误");
        var user = await _userService.GetUserbyId(_userContext.Id);
        if (!user.Item1)
            return Problem("用户不存在");
        user.Item2.Avatar = input.Value;
        return _userService.UpdateUsers(user.Item2) ? Success() : Problem("更新头像失败");
    }

    /// <summary>
    ///     设置/修改支付密码
    /// </summary>
    /// <returns></returns>
    [HttpPost("ReplenishPayPass")]
    public async Task<CommonResult> ReplenishPayPass(ReplenishPayPassDto dto)
        => await _userService.ReplenishPayPass(_userContext.Id, dto);

    /// <summary>
    ///     伙伴数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("Partner")]
    public CommonResult<PartnerVo> Partner()
        => _userService.Partner(_userContext.Id);

}